package com.dmm.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmm.result.Result;
import com.dmm.user.entity.Apply;
import com.dmm.user.entity.Userinfo;
import com.dmm.user.service.UserinfoService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com/dmm
 * @since 2023-04-02
 */
@RestController
@RequestMapping("/user/userinfo")
@CrossOrigin
public class UserinfoController {

    @Autowired
    UserinfoService userinfoService;

    @PostMapping("/getuserinfo")
    public Result getUserinfo( HttpServletRequest request){

        String token = request.getHeader("token");

        Userinfo userinfo=userinfoService.getUserinfo(token);

        return Result.success(userinfo);

    }

//修改
    @PutMapping("/revise")
    public Result revise(@RequestBody Userinfo userinfo){

        userinfoService.updateById(userinfo);
        return Result.success(null);

    }

    @ApiOperation("分页查询")
    @PostMapping("{page}/{limit}")
    public Result index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "Userinfo", value = "查询对象", required = false)
            // required = false传入参数可以为空
            @RequestBody(required = false) Userinfo userinfo) {
        //创建page对象，传递当前页和每页记录数
        Page<Userinfo> pageParam = new Page<>(page, limit);
        //获取条件值
        String name = userinfo.getName();//姓名
        String email = userinfo.getEmail();//邮箱
        String department = userinfo.getDepartment();//院系

        //封装条件
        QueryWrapper<Userinfo> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(name)) {
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(email)) {
            wrapper.like("email",email);
        }
        if(!StringUtils.isEmpty(department)) {
            wrapper.like("department",department);
        }

        //调用方法得到分页查询结果
        IPage<Userinfo> pageModel = userinfoService.page(pageParam, wrapper);
        return Result.success(pageModel);
    }


    //根据id获取信息
    @GetMapping("/getidbyinfo/{id}")
    public Result getidbyinfo(@PathVariable Long id){

        Userinfo byId = userinfoService.getById(id);
        return Result.success(byId);

    }

}

